import 'dart:math';

import 'package:flutter/material.dart';
//FIXME: point P 改成大写
class SolidpointPasswordWidget extends StatefulWidget {
  final String password;
  final Color inActiveColor;
  final Color activeColor;

  SolidpointPasswordWidget(
      {Key key, this.password, this.inActiveColor, this.activeColor})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _SolidpointPasswordWidgetState();
}

class _SolidpointPasswordWidgetState extends State<SolidpointPasswordWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      // color: Colors.green,
      width: MediaQuery.of(context).size.width - 30,
      height: 90,
      child: CustomPaint(
        painter: CustomPassword(widget.password,
            inActiveColor: widget.inActiveColor,
            activeColor: widget.activeColor),
      ),
    );
  }
}

class CustomPassword extends CustomPainter {
  String pwdLength;
  final Color inActiveColor;
  final Color activeColor;

  CustomPassword(this.pwdLength, {this.inActiveColor, this.activeColor});

  @override
  void paint(Canvas canvas, Size size) {
    Paint mPwdPaint;
//    FIXME: 小驼峰
    Paint mbackgroundPaint;
    mPwdPaint = Paint();
    mPwdPaint..color = activeColor ?? Colors.grey;

    mbackgroundPaint = Paint();
    mbackgroundPaint..color = inActiveColor ?? Colors.black;
    var per = size.width / 6.0;
    var half = per / 2;
    var radio = per / 4;
    mbackgroundPaint.style = PaintingStyle.fill;

    /// 默认背景
    for (var i = 0; i < 6; i++) {
      canvas.drawArc(
          Rect.fromLTRB(i * per + half - radio, size.height / 2 - radio,
              i * per + half + radio, size.height / 2 + radio),
          0.0,
          2 * pi,
          true,
          mbackgroundPaint);
    }

    /// 填充数量占位
    for (var i = 0; i < pwdLength.length && i < 6; i++) {
      canvas.drawArc(
          Rect.fromLTRB(i * per + half - radio, size.height / 2 - radio,
              i * per + half + radio, size.height / 2 + radio),
          0.0,
          2 * pi,
          true,
          mPwdPaint);
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}
